*******************************
* Table A1 
*******************************
clear
use masterdata_county.dta

encode state, gen(state2)
encode county, gen(county2)

gen senior = (pop_55above)/pop
gen whiteratio = white/pop
gen blackratio = black/pop
gen a1 = white/pop
gen a2 = black/pop
gen a3 = indian/pop
gen a4 = asian/pop
gen a5 = pacific/pop
gen a6 = otherace/pop
gen a7 = tworaces/pop
gen a8 = hispanic/pop
gen ethnic = 1 - (a1^2 + a2^2 + a3^2 + a4^2 + a5^2 + a6^2 + a7^2 +a8^2)
gen nohs = lessthanhs/pop_over25
gen hs = highschool/pop_over25 
gen loweredu = nohs + hs
gen college = bachelor/pop_over25
gen advanced = (master + professional + doctorate)/pop_over25
gen unemployment = civilian_unemployed/civilian
gen male_unemployment = male_umemployed/civilian_male
gen female_unemployment = female_umemployed/civilian_female
gen white_unemployment = whilte_unemployed/whilte_laborforce
gen publicsectorratio = public_sector/employed_civilian
gen manufacturingratio = manufacturing/employed_civilian
gen agricultureratio = agriculture/employed_civilian
gen foreignratio = foreign_born/pop
gen noinsuranceratio = noinsurance/insurance_pop

gen lpop = ln(pop +1)
gen lnpcincome = ln(pcincome+1)

gen repratio08 = gop_2008/(dem_2008 + gop_2008)
gen repratio12 = gop_2012/(dem_2012 + gop_2012)
gen repratio16 = gop_2016/(dem_2016 + gop_2016)

macro define demographic "lpop senior whiteratio loweredu lnpcincome unemployment white_unemployment manufacturingratio foreignratio noinsuranceratio"

gen lntotaffected = ln(totaffected +1)
gen lntotaffected_app = ln(totaffected_approved +1)
gen lntotaffected_den = ln(totaffected_denied +1)

gen taaworkforce = totaffected/civilian
gen taaworkforce_app = totaffected_approved/civilian
gen taaworkforce_denied = totaffected_denied/civilian

gen lntaaworkforce = ln(taaworkforce +1)

gen certifyratio = totaltaa_certified/totaltaa
replace certifyratio = 0 if certifyratio ==.

gen approvedratio = totaffected_approved/totaffected

gen repchange1 = (repratio16 - repratio08)*100
gen repchange2 = (repratio16 - repratio12)*100


tabstat rep_primary_trump_vs repchange1 repchange2 d_tradeusch_pw totaltaa lntotaffected_app certifyratio $demographic, stat(N mean sd min max) col(stat)

